package com.sigmatrix.h5.web.controller.utils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sigmatrix.h5.entity.v2.CodeFleeCheckRecord;
import com.sigmatrix.h5.entity.v2.CodeInspectionRecord;
import com.sigmatrix.h5.exception.MessageException;
import com.sigmatrix.h5.service.db.CodeFleeCheckRecordDBService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


@Api(tags="串货测试")
@RestController
@RequestMapping("/util/test/flee")
public class TestCodeFleeCheckRecordController {
	
	@Autowired
	private CodeFleeCheckRecordDBService codeFleeCheckRecordDBService;
	
	
	@PostMapping("/insert/esecode/{esecode}")
	public Integer insert(@PathVariable String esecode,@RequestBody CodeFleeCheckRecord record) {
		record.setTablePrefix("_"+esecode);
		Integer insertAndReturnId = codeFleeCheckRecordDBService.insertAndReturnId(record);
		System.err.println(insertAndReturnId);
		return record.getId();
	}
	@PostMapping("/insert2/esecode/{esecode}")
	public Integer insert2(@PathVariable String esecode,@RequestBody CodeFleeCheckRecord record) {
		record.setTablePrefix("_"+esecode);
		boolean issuccess = codeFleeCheckRecordDBService.insert(record);
		System.err.println(issuccess);
		return record.getId();
	}
	
	@ApiOperation(value="串货记录查询",response=CodeFleeCheckRecord.class)
	@GetMapping("/select/esecode/{esecode}/id/{ids}")
	public List<CodeFleeCheckRecord> select(@PathVariable String esecode,@PathVariable String ids) {
		String[] split = ids.split(",");
		if(split==null||split.length==0){
			throw new MessageException("参数不正确");
		}
		Set<Integer> idSet = new HashSet<>();

		try {
			for (String str : split) {
				idSet.add(Integer.valueOf(str));
			}
		} catch (NumberFormatException e) {
			throw new MessageException("参数不正确");
		}
		
		Integer[] idsInt = idSet.toArray(new Integer[0]);
		
		List<CodeFleeCheckRecord> list = codeFleeCheckRecordDBService.findByIds(idsInt, esecode);
		System.err.println(list);
		return list;
	}
	
}

